$(function(){
	showCondition(true);
	var scoreQuota = [{
		code : 'drg',
		name : 'DRG指标',
		children : [{
			name : "产能指标",
			children : [ {
				code : "TotalScore",
				name : "综合得分"
			}, {
				code : "CapacityScore",//科室产能得分
				name : "产能得分"
			}, {
				code : "EfficiencyScore",
				name : "效率得分"
			}, {
				code : "QualityScore",
				name : "安全得分"
			}]
		}]
	}]
	
	// getQuotas("ywcHomePage", loadData);
	function loadData(totalQuota){
		// 全院指标 (1.Hosp 2.All)
		loadHospQuota("Hosp",totalQuota,"hospQuotasId","ywcHomePageTotal");
	}
	
	// 加载全院DRGs指标散点图和折线图
	// loadHospQuotaEcharts();
	
	// 加载科室绩效综合评价
	// loadHomeScoreDepQuota(scoreQuota,"科室","depscorequotasDivId","depscore-ul");

	//加载科室绩效综合评价数据
	// loadHomeScoreDepQuotaData(scoreQuota,"dep");
	
	$('.ywc_cap').click(function (e) {
		var option={};
		option.fileName = 'capacity'
		option.datas=[
		  {
		    sheetData:[]
		  }
		];
		$(".ywc_cap_table tr").each(function(){
			var tr = [];
			$(this).find("td").each(function(){
				tr.push($(this).text());
			})
			option.datas[0].sheetData.push(tr)
		});
		var toExcel=new ExportJsonExcel(option);
		toExcel.saveExcel();
	})
	$('.ywc_eff').click(function (e) {
		var option={};
		option.fileName = 'efficiency'
		option.datas=[
		  {
		    sheetData:[]
		  }
		];
		$(".ywc_eff_table tr").each(function(){
			var tr = [];
			$(this).find("td").each(function(){
				tr.push($(this).text());
			})
			option.datas[0].sheetData.push(tr)
		});
		var toExcel=new ExportJsonExcel(option);
		toExcel.saveExcel();
	});
	$('.ywc_qua').click(function (e) {
		var option={};
		option.fileName = 'quality'
		option.datas=[
		  {
		    sheetData:[]
		  }
		];
		$(".ywc_qua_table tr").each(function(){
			var tr = [];
			$(this).find("td").each(function(){
				tr.push($(this).text());
			})
			option.datas[0].sheetData.push(tr)
		});
		var toExcel=new ExportJsonExcel(option);
		toExcel.saveExcel();
	});
});
/**
 *全院历年指标分布图
 */
function loadHospQuotaEcharts(){
	var isGetCc = "0";
	if ($("#searchcq").children().eq(3).find("div.ci a").eq(0).hasClass("active")) {
		isGetCc = "1";
	}
	// 产能下面的加载CMI/DRGs散点图
	getYwcHospQuotaByThreed(isGetCc,'CMI,DRGCount');
	// 效率下面的时间消耗指数费用消耗指数分布
	getYwcHospQuotaByThreed(isGetCc,'TimeEff,FeeEff');
	// 加载安全下面低风险中低风险死亡率
	getYwcHospQuotaByThreed(isGetCc,'Risk1Ratio,Risk2Ratio');
}
/**
 * 
 * @@param isGetCc
 * @@param quotaCode
 * @@param cnt
 * @void
 * @ Discription:
 * @ywcHomePage.js
 */
function getYwcHospQuotaByThreed(isGetCc,quotaCode){
	var param=drgUI.setAjaxData(tabId);
	param.monitorType='Hosp';
    param.quotaCode=quotaCode;
    param.isGetCc=isGetCc;
	$.post(contextPath + "/DrgsIndexCal/getTrendData.dhc",param).done(function(res) {
		var listMap=res.data;
			if(quotaCode=="CMI,DRGCount"){
				createCapChart(listMap,param.compareType);
			}else if(quotaCode=="TimeEff,FeeEff"){
				createTimeFeeChart(listMap,param.compareType);
			}else if(quotaCode=="Risk1Ratio,Risk2Ratio"){
			   createQuaChart(listMap,param.compareType);
			}
	});
};
/**
 * 
 * @@param listMap
 * @void
 * @ Discription:绘制产能图
 * @ywcHomePage.js
 */
function createCapChart(listMap,compareType){
	
	var data = listMap;
	paramTabPage[tabId].cmiDrg=data;
	var head = "<thead><tr><td>标题</td>";
	var thisPer = "<tr><td>本期 [CMI,DRG组数]</td>";
	var lastPer = "";
	if(compareType=='year'){
		lastPer = "<tr><td>同期 [CMI,DRG组数]</td>";
	}
	var xmin = {datax:9999,datay:0};
	var xmax = {datax:-1,datay:0};
	var ymin = {datax:0,datay:9999};
	var ymax = {datax:0,datay:-1};
	var chartParams={autoWidth:true,autoHeight:'auto',heightRatio:0.8,markPointConfig:{label:{normal:{formatter:'{a}'}},data:[]}};
	var markPointData = {};
	for(var i=0;i<data.length;i++){
		head += "<td>"+data[i].ddesc+"</td>";
		thisPer += "<td>["+(typeof(data[i].CMI)=="undefined"?"-":data[i].CMI)+","+(typeof(data[i].DRGCount)=="undefined"?"-":data[i].DRGCount)+"]</td>";
		if(compareType=='year'){
			lastPer += "<td>["+(typeof(data[i].CMIcc)=="undefined"?"-":data[i].CMIcc)+","+(typeof(data[i].DRGCountcc)=="undefined"?"-":data[i].DRGCountcc)+"]</td>";
		}
		if(typeof(data[i].CMI)!="undefined"&&typeof(data[i].DRGCount)!="undefined"&&data[i].CMI<xmin.datax){
			xmin.datax = data[i].CMI;
			xmin.datay = data[i].DRGCount;
			xmin.index = i;
		}
		if(typeof(data[i].CMI)!="undefined"&&typeof(data[i].DRGCount)!="undefined"&&data[i].DRGCount<ymin.datay){
			ymin.datax = data[i].CMI;
			ymin.datay = data[i].DRGCount;
			ymin.index = i;
		}
		if(typeof(data[i].CMI)!="undefined"&&typeof(data[i].DRGCount)!="undefined"&&data[i].CMI>xmax.datax){
			xmax.datax = data[i].CMI;
			xmax.datay = data[i].DRGCount;
			xmax.index = i;
		}
		if(typeof(data[i].CMI)!="undefined"&&typeof(data[i].DRGCount)!="undefined"&&data[i].DRGCount>ymax.datay){
			ymax.datax = data[i].CMI;
			ymax.datay = data[i].DRGCount;
			ymax.index = i;
		}
	}
	if(typeof(xmin.index)!="undefined"){
		markPointData[xmin.index]=xmin;
	}
	if(typeof(ymin.index)!="undefined"){
		markPointData[ymin.index]=ymin;
	}
	if(typeof(xmax.index)!="undefined"){
		markPointData[xmax.index]=xmax;
	}
	if(typeof(ymax.index)!="undefined"){
		markPointData[ymax.index]=ymax;
	}
	chartParams.markPointData = markPointData;
	head += "</tr></thead>";
	thisPer += "</tr>";
	if(compareType=='year'){
		lastPer += "</tr>";
	}
	$(".ywc_cap_table").append(head+"<tbody>"+thisPer+lastPer+"</tbody>")
	var xyAxis = {"xAxis": {"code": "CMI", "desc": "CMI值"}, "yAxis": {"code": "DRGCount", "desc": "DRGs组数"}}
	scatterChartYZHome($("#cap-chart .echartscq"), xyAxis, data, "CMI和DRGs组数分布",'',chartParams);	
};
/**
 * 
 * @@param listMap
 * @void
 * @ Discription:绘制效率
 * @ywcHomePage.js
 */
function createTimeFeeChart(listMap,compareType){		
	var data = listMap;
	paramTabPage[tabId].timeFee=data;
	var head = "<thead><tr><td>标题</td>";
	var thisPer = "<tr><td>本期 [时间消耗指数,费用消耗指数]</td>";
	var lastPer="";
	if(compareType=='year'){
		lastPer = "<tr><td>同期 [时间消耗指数,费用消耗指数]</td>";
	}
	var xmin = {datax:9999,datay:0};
	var xmax = {datax:-1,datay:0};
	var ymin = {datax:0,datay:9999};
	var ymax = {datax:0,datay:-1};
	var chartParams={autoWidth:true,autoHeight:'auto',heightRatio:0.8,markPointConfig:{label:{normal:{show:true,color:'#ccc',formatter:'{a}'}},data:[]}};
	var markPointData = {};
	for(var i=0;i<data.length;i++){
		head += "<td>"+data[i].ddesc+"</td>";
		thisPer += "<td>["+(typeof(data[i].TimeEff)=="undefined"?"-":data[i].TimeEff)+","+(typeof(data[i].FeeEff)=="undefined"?"-":data[i].FeeEff)+"]</td>";
		if(compareType=='year'){
			lastPer += "<td>["+(typeof(data[i].TimeEffcc)=="undefined"?"-":data[i].TimeEffcc)+","+(typeof(data[i].FeeEffcc)=="undefined"?"-":data[i].FeeEffcc)+"]</td>";
		}
		if(typeof(data[i].TimeEff)!="undefined"&&typeof(data[i].FeeEff)!="undefined"&&data[i].TimeEff<xmin.datax){
			xmin.datax = data[i].TimeEff;
			xmin.datay = data[i].FeeEff;
			xmin.index = i;
		}
		if(typeof(data[i].TimeEff)!="undefined"&&typeof(data[i].FeeEff)!="undefined"&&data[i].FeeEff<ymin.datay){
			ymin.datax = data[i].TimeEff;
			ymin.datay = data[i].FeeEff;
			ymin.index = i;
		}
		if(typeof(data[i].TimeEff)!="undefined"&&typeof(data[i].FeeEff)!="undefined"&&data[i].TimeEff>xmax.datax){
			xmax.datax = data[i].TimeEff;
			xmax.datay = data[i].FeeEff;
			xmax.index = i;
		}
		if(typeof(data[i].TimeEff)!="undefined"&&typeof(data[i].FeeEff)!="undefined"&&data[i].FeeEff>ymax.datay){
			ymax.datax = data[i].TimeEff;
			ymax.datay = data[i].FeeEff;
			ymax.index = i;
		}
	}
	if(typeof(xmin.index)!="undefined"){
		markPointData[xmin.index]=xmin;
	}
	if(typeof(ymin.index)!="undefined"){
		markPointData[ymin.index]=ymin;
	}
	if(typeof(xmax.index)!="undefined"){
		markPointData[xmax.index]=xmax;
	}
	if(typeof(ymax.index)!="undefined"){
		markPointData[ymax.index]=ymax;
	}
	chartParams.markPointData = markPointData;
	head += "</tr></thead>";
	thisPer += "</tr>";
	if(compareType=='year'){
		lastPer += "</tr>";
	}
	
	$(".ywc_eff_table").append(head+"<tbody>"+thisPer+lastPer+"</tbody>");
	var xyAxis = {"xAxis": {"code": "TimeEff", "desc": "时间消耗指数"}, "yAxis": {"code": "FeeEff", "desc": "费用消耗指数"}}
	scatterChartYZHome($("#eff-chart .echartscq"), xyAxis, data, "时间消耗指数和费用消耗指数分布",'',chartParams);
}
/**
 * 
 * @@param listMap
 * @void
 * @ Discription:绘制安全
 * @ywcHomePage.js
 */
function createQuaChart(listMap,compareType){
	var data = listMap;
	paramTabPage[tabId].risk=data;
	var head = "<thead><tr><td>标题</td>";
	var r1ThisPer = "<tr><td>本期 (低风险死亡率)</td>";
	var r1LastPer = "";
	var r2ThisPer = "<tr><td>本期 (中低风险死亡率)</td>";
	var r2LastPer = "";
	if(compareType=='year'){
		r1LastPer = "<tr><td>同期 (低风险死亡率)</td>";
		r2LastPer = "<tr><td>同期 (中低风险死亡率)</td>";
	}
	var data1 = [];
	var data2 = [];
	var echartData;
	for(var i=0;i<data.length;i++){
		data1.push({ddesc:data[i].ddesc,Risk1Ratio:data[i]["Risk1Ratio"]});
		data2.push({ddesc:data[i].ddesc,Risk2Ratio:data[i]["Risk2Ratio"]});
		head += "<td>"+data[i].ddesc+"</td>";
		r1ThisPer += "<td>"+(typeof(data[i].Risk1Ratio)=="undefined"?"-":data[i].Risk1Ratio)+"</td>";
		r2ThisPer += "<td>"+(typeof(data[i].Risk2Ratio)=="undefined"?"-":data[i].Risk2Ratio)+"</td>";
		if(compareType=='year'){
			r1LastPer += "<td>"+(typeof(data[i].Risk1Ratiocc)=="undefined"?"-":data[i].Risk1Ratiocc)+"</td>";
			r2LastPer += "<td>"+(typeof(data[i].Risk2Ratiocc)=="undefined"?"-":data[i].Risk2Ratiocc)+"</td>";
		}
	}
	echartData=[data1,data2];
	head += "</tr></thead>";
	r1ThisPer += "</tr>";
	r2ThisPer += "</tr>";
	if(compareType=='year'){
		r1LastPer += "</tr>";
		r2LastPer += "</tr>";
	}
	$(".ywc_qua_table").append(head+"<tbody>"+r1ThisPer+r1LastPer+r2ThisPer+r2LastPer+"</tbody>")
	var fommaterFunc=function(params,ticket,callback){
     	var r1 = params[0].data;
     	var r2 = params[1].data;
     	var r1cc = paramTabPage[tabId].risk[params[0].dataIndex].Risk1Ratiocc;
     	var r2cc = paramTabPage[tabId].risk[params[1].dataIndex].Risk2Ratiocc;
     	var tP = "";//本期
     	var lP = "";//同期
     	var mrType = paramTabPage[tabId].mrType;//时间类型
     	if(mrType == "M"){
     		tP = getYwcHomePageTime("M","t",params[0].name);
     		lP = getYwcHomePageTime("M","l",params[0].name);
 		}else{
     		tP = params[0].name;
     		lP = getYwcHomePageTime(mrType,"",tP);
 		}
     	r1 = typeof(r1)=="undefined"?"-":r1;
     	r2 = typeof(r2)=="undefined"?"-":r2;
     	r1cc = typeof(r1cc)=="undefined"?"-":r1cc;
     	r2cc = typeof(r2cc)=="undefined"?"-":r2cc;
     	return params[0].seriesName+"<br/>"+" 本期("+tP+"):"+r1+"<br/>"+" 同期("+lP+"):"+r1cc+" <br/>"+params[1].seriesName+" <br/>"+" 本期("+tP+"):"+r2+"<br/>"+" 同期("+lP+"):"+r2cc;  
     }
	if(!$("#searchcq").children().eq(3).find("div.ci a").eq(0).hasClass("active")){
		fommaterFunc=0;//不设置提示框
 	}
	var lines = [{"code": "Risk1Ratio", "desc": "低风险死亡率(%)", "data":[]}, {"code": "Risk2Ratio", "desc":"中低风险死亡率(%)", "data":[]}];
	var chartParams={fommaterFunc:fommaterFunc,unit:'%',legendPosition:'bottom',legendPositionVal:25,autoWidth:true,autoHeight:'auto',heightRatio:0.8,connectNulls:true,gridLeft:10};
	if(paramTabPage[tabId].mrType=='custom'){
		chartParams.gridLeft=30;
		chartParams.gridRight=55;
	}
	lineChartBasic($("#qua-chart .echartscq"), lines, echartData, "低风险死亡率和中低风险死亡率分布",'',chartParams);
}
/**
 * 拼接div
 * @param scoreQuota
 * @param monitorName
 * @param divId
 * @param ulId
 */
function loadHomeScoreDepQuota(scoreQuota,monitorName,divId,ulId){
	if(!monitorName) monitorName = ""
	if(paramTabPage[tabId].compareType=="year"){
		compareName = "同期";
	}else if(paramTabPage[tabId].compareType=="month"){
		compareName = "上期";
	}else if(paramTabPage[tabId].compareType=="manage"){
		compareName = "绩效";
	}else if(paramTabPage[tabId].compareType=="std"){
		compareName = "标杆";
	}
	var str = '';
	var ul='';	
	for(var o in scoreQuota){
		var QuotaCode = scoreQuota[o].children;
		//拼接ul
		if(scoreQuota.length>1){
		if(o == 0){
			ul+='<li role="presentation" class="active">';
		}else{
			ul+='<li role="presentation">';
		}
		ul+='<a href="#'+scoreQuota[o].code+'QuaId2'+tabId+'" role="tab" data-toggle="tab" class="top-tab">'+scoreQuota[o].name+'</a>';
		ul+='</li>';
		$("#"+tabId+" #"+ulId).html(ul);
		}
	//开始拼接div
		if(o == 0){
			str+='<div role="tabpanel" class="tab-pane active" id="'+scoreQuota[o].code+'QuaId2'+tabId+'">';
		}else{
			str+='<div role="tabpanel" class="tab-pane" id="'+scoreQuota[o].code+'QuaId2'+tabId+'">';
		}
		//拼接页签
		for(var o in QuotaCode){
			var quotas = QuotaCode[o];
			str+=' <div class="paddingd10"> ';
			for(var o1 in quotas.children){				
				var quota = quotas.children[o1];
				var quotaCode = quota.code;
				var quotaName = quota.name;
				
				if(o1 == 0){
					ul+='<li role="presentation" class="active">';
				}else{
					ul+='<li role="presentation">';
				}
				ul+='<a href="#'+quotaCode+'-tc'+tabId+'" role="tab" data-toggle="tab">'+quotaName+'</a> ';
				ul+='</li>';
				$("#"+tabId+" #"+ulId).html(ul);
			}
				
			str+='   <div class="clearfix"></div> ';
			//拼接右侧表格
			str+='   <div class="tab-content"> ';
			for(var o1 in quotas.children){
				var quota = quotas.children[o1];
				var quotaCode = quota.code;
				var quotaName = quota.name;
				if(o1 == 0){
					str+='       <div role="tabpanel" class="tab-pane active" compareName="'+compareName+'" monitorName="'+monitorName+'" quotaName="'+quotaName+'" quotaCode="'+quotaCode+'" id="'+quotaCode+'-tc'+tabId+'"> ';
				}else{
					str+='       <div role="tabpanel" class="tab-pane" compareName="'+compareName+'" monitorName="'+monitorName+'" quotaName="'+quotaName+'" quotaCode="'+quotaCode+'" id="'+quotaCode+'-tc'+tabId+'"> ';
				}
				str+='           <div class="row padding100"> ';
				str+='               <div class="col-xs-5"> ';
				str+='                   <div class="echartscq" style=""></div> ';
				str+='               </div> ';
				str+='               <div class="col-xs-7 dataChangeSel"> ';
				str+='                   <table id="'+quotaCode+'-table'+tabId+'" class="table table-striped" cellspacing="0" width="100%" role="grid" aria-describedby="example_info"> ';
				str+='                       <caption class="contaotalsccq">'+monitorName+quotaName+'</caption> ';
				str+='                       <thead> ';
				str+='                           <tr role="row"> ';
				str+='                               <th tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-sort="ascending">'+monitorName+'</th> ';
				str+='                               <th tabindex="0" aria-controls="example" rowspan="1" colspan="1">本期值</th> ';
				str+='                               <th tabindex="0" aria-controls="example" rowspan="1" colspan="1">'+compareName+'值</th> ';
				str+='                               <th tabindex="0" aria-controls="example" rowspan="1" colspan="1">增长率</th> ';
				str+='                               <th tabindex="0" aria-controls="example" rowspan="1" colspan="1">操作</th> ';
				str+='                           </tr> ';
				str+='                       </thead> ';
				str+='                       <tbody></tbody> ';
				str+='                   </table> ';
				str+='               </div> ';
				str+='           </div> ';
				str+='       </div> ';
			}
			str+='   </div> ';
			str+=' </div> ';
		}
		str+='</div>';
		
	}	
	$("#"+tabId+" #"+divId).html(str);
}
/**
 * 加载数据
 * @param totalQuota
 * @param monitorType
 * @param functionOne
 * @param operfunctionTwo
 */
function loadHomeScoreDepQuotaData(scoreQuota,monitorType,functionOne,operfunctionTwo){
	for(var o in scoreQuota){
		var quotaTypes = scoreQuota[o].children;
		for(var o in quotaTypes){
			var quotas = quotaTypes[o];
			for(var o1 in quotas.children){
				var quota = quotas.children[o1];
				var quotaCode = quota.code;
				var quotaName = quota.name;
				loadHomeScoreDepQuotaDatasub(quotaCode, quotaName,monitorType,functionOne,operfunctionTwo);//最后两个参数为本页面自定义的方法，当没有方法传递时传递空字符
				}
			}
	}
}
/**
 * 循环加载div数据
 * @param quotaCode
 * @param quotaName
 * @param monitorType
 * @param functionOne
 * @param operfunctionTwo
 */
function loadHomeScoreDepQuotaDatasub(quotaCode, quotaName,monitorType,functionOne,operfunctionTwo){	
		var param=drgUI.setAjaxData(tabId);
	    param.quotaCode=quotaCode;
	    param.monitorType=monitorType;
		$.post(contextPath+"/DrgsIndexCal/indexCal.dhc",param).done(function(result){
		var data=[];
		if(result.success&&result.data && result.data.length>0){
			var data = result.data;
		}
		 var columns = [ 
	                    {  "data": "ccode",
	                    	"render":function(obj, type, full) {
	                    	return full.ddesc;
	               	 	 	}
	                     },
	                     { "data": "QuotaValue",
	                    	 "render":function(obj, type, full){ 
	                    		 return formatNumber(full.QuotaValue);
	                    	 	}
	                     },
	                     { "data": "QuotaValuecc",
	                    	 "render":function(obj, type, full){ 
	                    		 return formatNumber(full.QuotaValuecc);
	                    	 	}
	                    },
	                     { "data": "increasing","type": "number-fate",
	                       "render":function(obj, type, full){
	                    		 return gridcompareValue(full.QuotaValue, full.QuotaValuecc);
	                    	 }
	                     },
	                     { "datag": "OPERATION",
	                       "render":function(obj, type, full){
	                    	   return "<a title='趋势' href=\"javascript:trend('"+full.ccode+"', '"+full.ddesc+"', '"+monitorType+"', '"+quotaCode+"', '"+quotaName+"')\" class='trend-link'><i class='fa fa-line-chart'></i></a>";
	                       }  
	                     }
	                 ];
		 var paramsDataTables={order:[],columnDefs:[{"orderable": false,"targets":[4] }]};
		 $("#"+quotaCode+"-table"+tabId).drgsDataTable(columns, data, paramsDataTables);		
		})
	}

function loadDep(code,desc){
	var param = jQuery.extend(true,{},paramTabPage[tabId]);
	param.depGroupCode = code;
	openTab('/pages/yizheng/ywcDep.jsp', desc+"|"+'科室','Dep'+code, 'Dep'+code,param);

}
//医政首页获取同期时间
/**
 * mrType 时间类型
 * type 本期同期标识
 * time 时间
 */
function getYwcHomePageTime(mrType,type,time){
	if(mrType=="M"){//月份
		var condition = $(".time_id").html();//查询条件的时间
		var year = condition.substring(1,5);//年
		if(type == "l")
		{//去年
			year = year-1;
		}
		return year+"年"+time;
	}else if(mrType=="MC"){//累计
		time = time.substring(0,4);
		time = time-1;
		return time+"年";
	}else{//自定义
		var yearstart = time.substring(0,4);
		var yearend = time.substring(8,12);
		return (yearstart-1)+"."+time.substring(5,7)+"-"+(yearend-1)+"."+time.substring(13);
	}
}